package cn.wolfcode.ssm.service.impl;

import cn.wolfcode.ssm.domain.CustomerTransfer;
import cn.wolfcode.ssm.domain.Employee;
import cn.wolfcode.ssm.mapper.CustomerMapper;
import cn.wolfcode.ssm.mapper.CustomerTransferMapper;
import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.service.ICustomerTransferService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerTransferServiceImpl implements ICustomerTransferService {

    @Autowired
    private CustomerTransferMapper customerTransferMapper;

    @Autowired
    private CustomerMapper customerMapper;
    @Override
    public void delete(Long id) {
        customerTransferMapper.deleteByPrimaryKey(id);
    }

    //因为移交一次就是创建一条新的记录
    @Override
    public void save(CustomerTransfer customerTransfer) {
        //得到客户id
        Long customerId = customerTransfer.getCustomer().getId();
        //得到新的销售员id
        Long sellerId = customerTransfer.getNewSeller().getId();
        //把客户的旧有销售人员改为新的销售人员(指定对哪个客户改为哪个销售人员)
        customerMapper.updateSeller(customerId,sellerId);
        //得到当前登录用户
        Employee employee = (Employee)SecurityUtils.getSubject().getPrincipal();
        //设置操作人为当前登录用户
        customerTransfer.setOperator(employee);
        //设置操作时间为当前系统时间
        customerTransfer.setOperateTime(new Date());
        //执行移交操作
        customerTransferMapper.insert(customerTransfer);
    }

    @Override
    public CustomerTransfer getOne(Long id) {
        return customerTransferMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CustomerTransfer> getAll() {
        return customerTransferMapper.selectAll();
    }

    @Override
    public void update(CustomerTransfer customerTransfer) {
        customerTransferMapper.updateByPrimaryKey(customerTransfer);
    }


    @Override
    public PageInfo<CustomerTransfer> selctForPageResult(QueryObject qo) {
        //使用分页插件,传入当前页,每页显示数量
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<CustomerTransfer> customerTransfers = customerTransferMapper.selectForResult(qo);
        return new PageInfo(customerTransfers);
    }
}
